//
//  PrevueViewController.m
//  Streaming
//
//  Created by 韩一博 on 15/6/29.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "PrevueViewController.h"

@interface PrevueViewController ()

@end

@implementation PrevueViewController

- (void)dealloc
{
    [self.tableView release];
    [self.allArray release];
    [self.allView release];
    [self.allViewModel release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [[DateHandel shareHandle]createPrevueTable];
    self.navigationItem.title = @"预告片";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    
    [self createTableView];
    [self afnGetRequest];
    [self allAfnGetRequest];
    [self headerRefesh];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStyleGrouped];
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    if (WIDTH == 320 && HEIGHT == 568) {
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }
    if (WIDTH == 414 && HEIGHT == 736) {
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
    }
    if (WIDTH == 375 && HEIGHT == 667){
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }

    self.allView.allViewDelegate = self;
    NSLog(@"self.allView = %@", self.allViewModel);
    self.tableView.tableHeaderView = self.allView;
    [self.allView release];
    [self.tableView release];
    

}

- (void)presentAllView
{
    ButtonViewController *button = [[ButtonViewController alloc] init];
    button.movieUrl = self.allViewModel.allMp4Url;
    [self.navigationController presentViewController:button animated:YES completion:^{
        
    }];
    [button release];
    
}

-(void)headerRefesh
{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
        
        if (WIDTH == 320 && HEIGHT == 568) {
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        [self allAfnGetRequest];
        self.allView.allViewDelegate = self;
        NSLog(@"self.allView = %@", self.allViewModel);
        self.tableView.tableHeaderView = self.allView;
        [self.allView release];
    }];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"prevueCell";
    PrevueTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[PrevueTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    Prevue *prevue = [self.allArray objectAtIndex:indexPath.row];
//    NSLog(@"prevue = %@", prevue);
    cell.prevue = prevue;
    return cell;
}

- (void)afnGetRequest
{
    self.allArray = [NSMutableArray array];
    self.allArray = [[DateHandel shareHandle]selectAllPrevue];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/TrailerList.api";
//    NSLog(@"url = %@", url_string);
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        [[DateHandel shareHandle]dropPrevueTable];
        [[DateHandel shareHandle]createPrevueTable];
        [self.allArray removeAllObjects];

        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"trailers"];
//        NSLog(@"array = %@", array);
        for (NSMutableDictionary *dic in array) {
            Prevue *prevue = [[Prevue alloc] init];
            prevue.PrevueID = [[dic objectForKey:@"id"]integerValue];
            prevue.movieName = [dic objectForKey:@"movieName"];
            prevue.coverImg = [dic objectForKey:@"coverImg"];
            prevue.summary = [dic objectForKey:@"summary"];
            prevue.url = [dic objectForKey:@"url"];
            
            [self.allArray addObject:prevue];
//            NSLog(@"self.allArray = %@", self.allArray);
            [[DateHandel shareHandle]insertPrevue:prevue];
            [prevue autorelease];
        }
        
        [self.tableView reloadData];
        
        
        [self.HUD hide:YES];
        [self.tableView.footer endRefreshing];
     
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert autorelease];

        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];

    
}

- (void)allAfnGetRequest
{
        self.allViewModel = [[AllViewModel alloc] init];
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
            NSLog(@"url = %@", url_string);
    
        //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
            //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
            NSMutableDictionary *mineDic = [NSMutableDictionary dictionary];
            mineDic = [responseObject objectForKey:@"trailer"];
//            NSLog(@"mineDic = %@", mineDic);
                self.allViewModel.allTitle = [mineDic objectForKey:@"title"];
            NSLog(@"title = %@", self.allViewModel.allTitle);
                self.allViewModel.allImageUrl = [mineDic objectForKey:@"imageUrl"];
                self.allViewModel.allMp4Url = [mineDic objectForKey:@"mp4Url"];
                self.allViewModel.allType = [[mineDic objectForKey:@"type"]integerValue];
            NSLog(@"self = %@", self.allViewModel);
            self.allView.AllModel = self.allViewModel;
            
            [self.tableView reloadData];
            
            
            [self.HUD hide:YES];
            [self.tableView.footer endRefreshing];
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"失败==== %@",error);
            [self.HUD hide:YES];
        }];
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    ButtonViewController *button = [[ButtonViewController alloc] init];
    Prevue *prevue = [self.allArray objectAtIndex:indexPath.row];
    button.movieUrl = prevue.url;
    [self.navigationController presentViewController:button animated:YES completion:^{
        
    }];
    [button release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
